<%--
  Created by IntelliJ IDEA.
  User: Li Chuanwei
  Date: 2023-03-27
  Time: 0:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%--Sports Activities Management System--%>
    <title>并夕夕购物平台</title>
    <!-- 导入layui的css -->
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/layui/css/layui.css">

    <!-- 导入layui的js -->
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>

    <!-- 导入验证码的css -->
    <link rel="stylesheet" type="text/css"
          href="${pageContext.request.contextPath}/static/slidercaptcha/slidercaptcha.css">

    <!-- 导入验证码的js -->
    <script src="${pageContext.request.contextPath}/static/slidercaptcha/longbow.slidercaptcha.js"></script>

    <!-- 导入图标的css -->
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/fontawesome/css/all.css">

    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/login.css">

    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>

    <!-- 导入时间的js -->
    <script src="${pageContext.request.contextPath}/static/js/time.js" defer="true"></script>

</head>
<body>
<%--头部--%>
<header>
    <img src="${pageContext.request.contextPath}/static/img/school.png" alt="school"/>
    <div class="logo">
        <h6>Nanchang Hangkong University<br>
            Shopping Management System</h6>
    </div>
    <div class="line"></div>
    <h2 class="brand">并夕夕购物平台后台管理系统</h2>
    <div class="time">
        <h6 class="time"></h6>
    </div>
</header>
<%--中间内容--%>
<div class="main">
    <div id="login" class="login-class">
        <form class="layui-form" action="" method="post">
            <div class="login-content">
                <div class="login-title">
                    <img class="login--title-img"
                         src="${pageContext.request.contextPath}/static/img/login-title-icon.ico" alt="">
                    <div class="login-title-text">并夕夕系统后台</div>
                </div>
                <!-- tabs切换开始 -->
                <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                    <ul class="layui-tab-title div-tab-title">
                        <li class="layui-this">账号密码登录</li>
                        <li>手机登录</li>
                    </ul>
                    <div class="layui-tab-content" data-index="0">
                        <!-- 账号登录、 -->
                        <div class="layui-tab-item layui-show">

                            <div class="layui-form-item">
                                <div class="layui-input-inline">
                                    <i class="layui-icon layui-icon-username"></i>
                                    <input type="text" name="account" id="account" placeholder="账号"
                                           lay-verify="accountverify" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-inline">
                                    <i class="layui-icon layui-icon-password"></i>
                                    <input type="password" name="password" id="password" placeholder="密码"
                                           lay-verify="passverify" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <!-- 手机登录 -->
                        <div class="layui-tab-item">
                            <div class="layui-form-item">
                                <div class="layui-input-inline">
                                    <i class="layui-icon layui-icon-cellphone"></i>
                                    <input type="text" name="phone" id="phone" placeholder="手机号"
                                           lay-verify="phoneverify" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-inline-code">
                                    <div class="layui-input-inline-code-input">
                                        <i class="layui-icon layui-icon-vercode"></i>
                                        <input type="text" name="code" id="code" style="width:150px;"
                                               placeholder="验证码" autocomplete="off" class="layui-input">
                                    </div>
                                    <div class="login-code-btn">
                                        <button type="button" class="layui-btn layui-btn-primary" id="getCode"
                                                onclick="sendMessage()">获取验证码
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
                <!-- tabs切换结束 -->
                <div class="tip"><a href="findpassword.html" class="save-tip">忘记密码</a></div>
                <!-- 登录按钮 -->
                <div class="foot-form-item">
                    <button class="layui-btn layui-btn-normal login-btn" lay-submit lay-filter="login">登录</button>
                </div>
            </div>
        </form>
    </div>
</div>
<%--尾部内容--%>
<footer id="foot">版权所有© 2023南昌航空大学并夕夕系统后台系统-212061-01</footer>
<script>

    var sms = "";

    function sendMessage() {
        var isCaptcha = false;
        var btn = document.getElementById("getCode");
        var mobileInput = document.getElementById("phone");
        var countdown = 60; // 倒计时时间（单位：秒）

        // 验证手机号是否合法
        var phonePattern = /^1\d{10}$/;
        var phoneNumber = mobileInput.value.trim();
        if (!phonePattern.test(phoneNumber)) {
            layer.msg('手机号格式错误', {icon: 2}, function () {
            });
            return;
        }

        layer.open({
            type: 1, // page 层类型
            area: ['400px', '300px'],
            title: '请完成滑块验证码',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: '<div id="captchaContainer"></div>',
            success: function (layero, index) {
                // 弹出层加载成功后，在其中初始化 SliderCaptcha
                var captcha = sliderCaptcha({
                    id: 'captchaContainer',
                    height: 200,
                    setSrc: function () {
                        return 'static/img/Pic' + Math.round(Math.random() * 4) + '.jpg';
                    },
                    onSuccess: function () {
                        isCaptcha = true;
                        layer.closeAll();
                        $.ajax({
                            url: "${pageContext.request.contextPath}/getCode",
                            type: "post",
                            data: {
                                phone: phoneNumber,
                            },
                            success: function (result) {
                                sms = result.message;
                            },
                            error: function () {
                                layer.msg('验证码发送失败', {icon: 2}, function () {
                                });
                                return;
                            }
                        });
                        // 禁用按钮
                        btn.disabled = true;

                        // 更新按钮文本，并开始倒计时
                        btn.innerHTML = countdown + " 秒后获取";
                        var timer = setInterval(function () {
                            countdown--;
                            if (countdown > 0) {
                                btn.innerHTML = countdown + " 秒后获取";
                            } else {
                                clearInterval(timer);
                                btn.innerHTML = "获取验证码";
                                btn.disabled = false;
                            }
                        }, 1000); // 每秒更新一次倒计时

                        // 在定时器结束前禁止再次点击按钮
                        setTimeout(function () {
                            clearInterval(timer);
                            btn.innerHTML = "获取验证码";
                            btn.disabled = false;
                        }, countdown * 1000);
                    }
                });
            }
        });
    }

    layui.use(['form', 'element', 'layer', 'jquery'], function () {
        var element = layui.element;
        var form = layui.form;
        var $ = layui.jquery;

        //一些事件
        element.on('tab(docDemoTabBrief)', function (data) {

            if (data.index == 0) {
                $('#phone').val('')
                $('#code').val('')
                $(".layui-tab-content").attr('data-index', 0)
            } else {
                $('#account').val('')
                $('#password').val('')
                $(".layui-tab-content").attr('data-index', 1)

            }
        });
        form.verify({
            //账号
            accountverify: function (value, item) { //value：表单的值、item：表单的DOM对象
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 0) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                    if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                        return '用户名不能有特殊字符';
                    }
                    if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                        return '用户名首尾不能出现下划线\'_\'';
                    }
                }
            }

            //密码
            , passverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 0) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                    if (!/^\w+$/.test(value)) {
                        return '只能输入数字和字母'
                    }
                    if (!/^[A-Za-z0-9]{6,16}$/.test(value)) {
                        return '密码必须6到16位，且不能出现空格'
                    }
                }
            },
            //手机号
            phoneverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 1) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                    if (!/^1[3-9]\d{9}$/.test(value)) {
                        return '请输入正确的手机号';
                    }
                }
            },
            //验证码
            codeverify: function (value, item) {
                let Typeindex = $(".layui-tab-content").attr('data-index')
                if (Typeindex == 1) {
                    if (!value) {
                        return '输入框不能为空';
                    }
                }
            }
        });

        form.on('submit(login)', function (data) {
            let Typeindex = $(".layui-tab-content").attr('data-index');
            if (Typeindex == 1) {
                if (data.field.code !== sms){
                    layer.msg('验证码输入错误', {icon: 2}, function () {
                    });
                    return false;
                }
            }

            var index = layer.load(0, {
                shade: [0.3, '#fff'],
            });

            var formData = data.field;
            // 发送Ajax请求并处理结果
            $.ajax({
                url: '${pageContext.request.contextPath}/manager/loginIn',
                type: 'POST',
                data: formData,
                success: function(res){
                    layer.close(index);//关闭弹窗
                    if(res.code == 0 && Typeindex == 0)
                        layer.msg("账号密码错误");
                    else if(res.code == 0 && Typeindex == 1)
                        layer.msg("手机号对应的账户不存在");
                    else if(res.code == 200)
                        window.location.href = "${pageContext.request.contextPath}/mto_index";
                },
                error: function(err){
                    layer.close(index);//关闭弹窗
                    layer.msg("请求出现异常");
                }
            });
            return false;
        });
    });

</script>
</body>
</html>